package net.tngou.iask.service;


import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tngou.iask.dao.MedicalDao;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Statisticmedical;

/**
 * 医学统计
 * @author tngou@tngou.net
 *
 */
public class MedicalService extends BaseService {

	/**
	 * 
	 * @param page
	 * @param size
	 * @param main
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main) {
		MedicalDao dao = new MedicalDao();
		return dao.getPage(page, size, main);
	}
	
	
	/**
	 * 
	 * @param main
	 */
	@SuppressWarnings("serial")
	public void save(long main) {
		DiseaseService diseaseService = new DiseaseService();
		List<Map<String, Object>> diseases = diseaseService.getList(main);//取得疾病
		Statisticmedical bean = new Statisticmedical();
		diseases.forEach(e->{
			Statisticmedical medical=bean.get(new HashMap<String,Object>(){{put("main",main);put("disease",e.get("id"));}});
			if(medical==null)
			{
				medical = new Statisticmedical();
				medical.setDisease(Long.parseLong(e.get("id").toString()));
				medical.setMain(main);
				medical.save();
			}
			_statistic(medical);
		});
		
	}


	/**
	 * 统计
	 * @param medical
	 */
	private void _statistic(Statisticmedical medical) {
		
		MedicalDao dao = new MedicalDao();
		medical.setPatientcount(dao.patientcount(medical.getMain(), medical.getDisease()));
		medical.setCurecount(dao.curecount(medical.getMain(), medical.getDisease()));
		medical.setAccesscount(dao.accesscount(medical.getMain(), medical.getDisease()));
		if(medical.getPatientcount()>0&&medical.getCurecount()>0)//治愈率 
		{
			float curerate =dao.curecount(medical.getMain(), medical.getDisease(),1);
			medical.setCurerate(curerate*100/(float)medical.getCurecount());	
		}
		if(medical.getPatientcount()>0&&medical.getCurecount()>0)//有效率 复发率 
		{
			float effectiverate = dao.effective(medical.getMain(), medical.getDisease());
			medical.setEffectiverate(effectiverate*100/(float)medical.getPatientcount());
		}
		if(medical.getPatientcount()>0&&medical.getCurecount()>0)// 复发率 
		{
			float recurrencerate = medical.getCurecount()-medical.getPatientcount();
			medical.setRecurrencerate(recurrencerate*100/(float)medical.getCurecount());;
		}
		if(medical.getPatientcount()>0&&medical.getCurecount()>0)// 死亡率 
		{
			float mortality = dao.mortality(medical.getMain(), medical.getDisease());
			medical.setMortality(mortality*100/(float)medical.getPatientcount());
		}
		if(medical.getPatientcount()>0&&medical.getCurecount()>0)// 随访接受率
		{
			float accessrate = dao.accesscount(medical.getMain(), medical.getDisease(),2);
			medical.setAccessrate(accessrate*100/(float)medical.getPatientcount());
		}
		if(medical.getPatientcount()>0&&medical.getCurecount()>0)// 生成率
		{
			
			medical.setGenerationrate_n1(generationrate(medical.getMain(),medical.getDisease(),1));
			medical.setGenerationrate_n3(generationrate(medical.getMain(),medical.getDisease(),3));			
			medical.setGenerationrate_n5(generationrate(medical.getMain(),medical.getDisease(),5));		
			medical.setGenerationrate_n10(generationrate(medical.getMain(),medical.getDisease(),10));
			
		}
		medical.setTime(new Timestamp(new Date().getTime()));//设置更新时间
		medical.update();
	}
	
	
	/**
	 * 生成率
	 * @param main
	 * @param disease
	 * @param year
	 * @return
	 */
	public float generationrate(long main,long disease,int year) {
		
		MedicalDao dao = new MedicalDao();
		float generationrate=dao.generationtotal(main, disease, year);
		if(generationrate>0)
		{
			float generation=generationrate-dao.generation(main, disease, year);
			generationrate=(float)(generation*100/generationrate);
		}
		return generationrate;
	}
	
}
